package com.hck.domain.doctor.service;

import com.hck.api.model.DoctorAuthenticateRequestDTO;
import com.hck.api.model.DoctorUpdatePasswordRequestDTO;
import com.hck.api.model.DoctorUpdateProfileRequestDTO;
import com.hck.domain.doctor.model.entity.DoctorEntity;
import com.hck.domain.doctor.model.valobj.LoginRequest;
import com.hck.domain.doctor.model.valobj.LoginResult;
import com.hck.domain.doctor.repository.IDoctorRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * DoctorDomainService class description here.
 *
 * @author 黄朝看
 * @date 2025/7/2
 */
@Service
public class DoctorDomainService {

    @Resource
    private IDoctorRepository repository;

    public LoginResult login(LoginRequest loginRequest) {
        //验证参数
        if(!loginRequest.isValid())
            return LoginResult.failure("账号或密码不能为空");

        //根据账号获取医生信息
        DoctorEntity entity = repository.findByLoginName(loginRequest.getLoginName());

        if(entity == null)
            return LoginResult.failure("账号不存在");

        //检查账号状态
        if(!entity.canLogin())
            return LoginResult.failure("账号已禁用");

        if(!entity.validatePassword(loginRequest.getPassword(),entity.getPassword()))
            return LoginResult.failure("密码错误");

        entity.updateLoginInfo();
        repository.update(entity);

        return LoginResult.success(entity.getDoctorId(), entity.getLoginName(), entity.getName());
    }

    public DoctorEntity getDoctorInfo(Long doctorId) {
        return repository.getDoctorInfo(doctorId);
    }

    public List<DoctorEntity> getAllDoctors() {
        return repository.findAll();
    }

    /**
     * 医生密码修改
     */
    public boolean updatePassword(DoctorUpdatePasswordRequestDTO requestDTO) {
        // 验证原密码
        DoctorEntity doctor = repository.getDoctorInfo(requestDTO.getDoctorId());
        if (doctor == null) {
            return false;
        }

        if (!doctor.validatePassword(requestDTO.getOldPassword(), doctor.getPassword())) {
            return false;
        }

        // 更新密码
        DoctorEntity updateEntity = new DoctorEntity();
        updateEntity.setDoctorId(requestDTO.getDoctorId());
        updateEntity.setPassword(requestDTO.getNewPassword());
        repository.update(updateEntity);

        return true;
    }

    /**
     * 医生信息修改
     */
    public boolean updateProfile(DoctorUpdateProfileRequestDTO requestDTO) {
        // 获取原有医生信息
        DoctorEntity doctor = repository.getDoctorInfo(requestDTO.getDoctorId());
        if (doctor == null) {
            return false;
        }

        // 更新可编辑字段
        if (requestDTO.getRealName() != null) doctor.setName(requestDTO.getRealName());
        if (requestDTO.getDepartment() != null) doctor.setDepartment(requestDTO.getDepartment());
        if (requestDTO.getTitle() != null) doctor.setTitle(requestDTO.getTitle());
        if (requestDTO.getHospital() != null) doctor.setHospital(requestDTO.getHospital());
        if (requestDTO.getPhone() != null) doctor.setPhone(requestDTO.getPhone());
        if (requestDTO.getEmail() != null) doctor.setEmail(requestDTO.getEmail());
        if (requestDTO.getIntroduction() != null) doctor.setIntroduction(requestDTO.getIntroduction());
        if (requestDTO.getAvatar() != null) doctor.setAvatar(requestDTO.getAvatar());

        // 调用仓储更新
        repository.update(doctor);

        return true;
    }

    /**
     * 医生身份验证
     */
    public boolean authentic(DoctorAuthenticateRequestDTO requestDTO) {
        DoctorEntity doctor = repository.getDoctorInfo(requestDTO.getDoctorId());
        if (doctor == null) {
            return false;
        }
        return doctor.validatePassword(requestDTO.getPassword(), doctor.getPassword());
    }
}
